package com.stx.servlet;

import java.io.IOException;
import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.stx.entity.TMedicine;
import com.stx.jdbc.JdbcUtil;

/**
 * Servlet implementation class dmeditservlet
 */
public class dmeditservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public dmeditservlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String mid=request.getParameter("mid");
		String mname=request.getParameter("mname");
		String mtypeid=request.getParameter("mtypeid");
		String mformid=request.getParameter("mformid");
		String mfactoryid=request.getParameter("mfactoryid");
		String prescription=request.getParameter("prescription");
		String expiration=request.getParameter("expiration");
		String mcount=request.getParameter("mcount");
		String mfunction=request.getParameter("mfunction");
		 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		   try {
			Date date = null;
			date = format.parse(expiration);
		} catch (ParseException e1) {
			
			e1.printStackTrace();
		}
		 java.sql.Date sqlDate=new java.sql.Date(new java.util.Date().getTime());
		String did=(String)request.getSession().getAttribute("did");
		List<TMedicine> medlist=new ArrayList<TMedicine>();
		Connection con=null;
		PreparedStatement pst=null;
		try {
			con=JdbcUtil.getCon();
			pst=con.prepareStatement("update tbl_medicine set mname=?,mtypeid=?,mformid=?,mfactoryid=?,prescription=?,expiration=?, mcount=?,mfunction=?  where mid=?");
		    pst.setString(1, mname);
		    pst.setString(2, mtypeid);
		    pst.setString(3, mformid);
		    pst.setString(4, mfactoryid);
		    pst.setString(5, prescription);
		    pst.setDate(6, sqlDate);
		    pst.setString(7, mcount);
		    pst.setString(8, mfunction);
		    pst.setString(9, mid);
		    pst.executeQuery();
		    
		    pst=con.prepareStatement("select  mid ,mname,m.mtypeid,mformid,mfactoryid,prescription,expiration, mcount,mfunction from tbl_prescription p,tbl_department d,tbl_medicine m where p.did=d.did and p.mtypeid=m.mtypeid");
//			pst.setString(1, did);
			ResultSet rs =pst.executeQuery();
			while(rs.next()){
				TMedicine med=new TMedicine();
				med.setMcount(rs.getString("mcount"));
				med.setMfactoryid(rs.getString("mfactoryid"));
				med.setMformid(rs.getString("mformid"));
				med.setMfunction(rs.getString("mfunction"));
				med.setMid(rs.getString("mid"));
				med.setMname(rs.getString("mname"));
				med.setMtypeid(rs.getString("mtypeid"));
				med.setExpiration(rs.getString("expiration"));
				med.setPrescription(rs.getString("prescription"));
				medlist.add(med);
		}} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}finally {
			JdbcUtil.closeCon(con, pst);
		}
		 request.setAttribute("medlist",medlist);
		 request.getRequestDispatcher("/admin/showdm.jsp").forward(request, response);
	}

}
